//
//  GGTAppDelegate.m
//  GGT
//
//  Created by Vision on 14-10-27.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTAppDelegate.h"
#import "GGTTeamBuyViewController.h"
#import "GGTMapViewController.h"
#import "GGTSearchViewController.h"
#import "GGTUserViewController.h"

//微博相关
@interface WBBaseResponse ()



- (void)debugPrint;
@end

@implementation GGTAppDelegate
+ (GGTAppDelegate *)instance {
	return (GGTAppDelegate *)[[UIApplication sharedApplication] delegate];
}
//豆瓣API相关
- (id)init {
	self = [super init];
    if (self) {
        _dpapi = [[DPAPI alloc] init];
		_appKey = [[NSUserDefaults standardUserDefaults] valueForKey:@"appkey"];
		if (_appKey.length<1) {
			_appKey = kDPAppKey;
		}
		_appSecret = [[NSUserDefaults standardUserDefaults] valueForKey:@"appsecret"];
		if (_appSecret.length<1) {
			_appSecret = kDPAppSecret;
		}
    }
    return self;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //微博SDK
    [WeiboSDK registerApp:@"3870232803"];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UITabBarController *tabVC = [[UITabBarController alloc]init];
    //团购页面(主页面)
    GGTTeamBuyViewController *teamBuyVC = [[GGTTeamBuyViewController alloc]init];
    UINavigationController *teamBuyNavVC = [[UINavigationController alloc]initWithRootViewController:teamBuyVC];
    self.navigationControllerDelegate = [[DDHNavigationControllerDelegate alloc] initWithTransitionType:0];
    teamBuyNavVC.delegate = self.navigationControllerDelegate;
    teamBuyVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"搞个团" image:[UIImage imageNamed:@"homeB.png"] selectedImage:[[UIImage imageNamed:@"homeY.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    teamBuyVC.tabBarItem.badgeValue = @"新";
    //地图页面
    GGTMapViewController *mapVC = [[GGTMapViewController alloc]init];
    mapVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"地图" image:[UIImage imageNamed:@"mapB.png"] selectedImage:[[UIImage imageNamed:@"mapY.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    //搜索页面
    GGTSearchViewController *searchVC = [[GGTSearchViewController alloc]init];
    UINavigationController *searchNavVC = [[UINavigationController alloc]initWithRootViewController:searchVC];
    self.navigationControllerDelegate = [[DDHNavigationControllerDelegate alloc] initWithTransitionType:0];
    searchNavVC.delegate = self.navigationControllerDelegate;
    searchNavVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"搜索" image:[UIImage imageNamed:@"searchB.png"] selectedImage:[[UIImage imageNamed:@"searchY.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    //用户页面
    GGTUserViewController *userVC  =[[GGTUserViewController alloc]init];
    UINavigationController *userNavVC = [[UINavigationController alloc]initWithRootViewController:userVC];
    self.navigationControllerDelegate = [[DDHNavigationControllerDelegate alloc] initWithTransitionType:0];
    userNavVC.delegate = self.navigationControllerDelegate;
    userVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"用户" image:[UIImage imageNamed:@"userB.png"] selectedImage:[[UIImage imageNamed:@"userY.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    tabVC.viewControllers = @[teamBuyNavVC, mapVC, searchNavVC, userNavVC];
    tabVC.delegate = self;
    self.window.rootViewController = tabVC;
    [teamBuyNavVC release];
    [teamBuyVC release];
    [mapVC release];
    [searchVC release];
    [searchNavVC release];
    [userVC release];
    [tabVC release];
    return YES;
}
//tab切换重置
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController*)viewController popToRootViewControllerAnimated:YES];
    }
    viewController.tabBarItem.badgeValue = nil;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    //    if ([request isKindOfClass:WBProvideMessageForWeiboRequest.class])
    //    {
    //        ProvideMessageForWeiboViewController *controller = [[[ProvideMessageForWeiboViewController alloc] init] autorelease];
    //        [self.viewController presentViewController:controller animated:YES completion:^{
    //
    //        }];
    //        //  [self.viewController presentModalViewController:controller animated:YES];
    //    }
}
//微博登陆回调函数
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    //数据库记录UserAccessToken
    NSString *userAccessToken =[(WBAuthorizeResponse *)response accessToken];
    [[NSUserDefaults standardUserDefaults]setObject:userAccessToken forKey:@"UserAccessToken"];
    //数据库记录userID
    NSString *userID = [(WBAuthorizeResponse *)response userID];
    [[NSUserDefaults standardUserDefaults]setObject:userID forKey:@"userID"];
    //强制写入
    [[NSUserDefaults standardUserDefaults]synchronize];
    //建立通知中心控制登陆状态
    if (0 ==(int)response.statusCode) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"userLoading" object:nil userInfo:nil];
    }
    if (-1 == (int)response.statusCode) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"loadButtonTitle" object:nil userInfo:nil];
        
    }
    
    
    //    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
    //    {
    //        NSString *title = @"发送结果";
    //        NSString *message = [NSString stringWithFormat:@"响应状态: %d\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",(int)response.statusCode, response.userInfo, response.requestUserInfo];
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
    //                                                        message:message
    //                                                       delegate:nil
    //                                              cancelButtonTitle:@"确定"
    //                                              otherButtonTitles:nil];
    //        [alert show];
    //        [alert release];
    //    }
    //    else if ([response isKindOfClass:WBAuthorizeResponse.class])
    //    {
    //        NSString *title = @"认证结果";
    //        NSString *message = [NSString stringWithFormat:@"响应状态: %d\nresponse.userId: %@\nresponse.accessToken: %@\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",(int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken], response.userInfo, response.requestUserInfo];
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
    //                                                        message:message
    //                                                       delegate:nil
    //                                              cancelButtonTitle:@"确定"
    //                                              otherButtonTitles:nil];
    //
    //        self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
    //
    //        [alert show];
    //        [alert release];
    //    }
    if ((int)response.statusCode == 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GGT"
                                                        message:@"随时随地搞个团!"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"登陆失败,请登陆以体验更多功能"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
